/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.evl.tecinfo.gui;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Tecinfo
 */
public class Cargo_gui extends javax.swing.JFrame {
    
    int linha_selec = -1;

    /**
     * Creates new form Cargo
     */
    public Cargo_gui() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bt_rem_cargo = new javax.swing.JButton();
        bt_lim_cargo = new javax.swing.JButton();
        jl_cad_carg = new javax.swing.JLabel();
        jl_nome_cargo = new javax.swing.JLabel();
        jl_cod_cargo = new javax.swing.JLabel();
        bt_cad_cargo = new javax.swing.JButton();
        ct_nome_cargo = new javax.swing.JTextField();
        ct_cod_cargo = new javax.swing.JTextField();
        bt_edit_cargo = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tb_cargo = new javax.swing.JTable();

        bt_rem_cargo.setText("Remover");
        bt_rem_cargo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_rem_cargoActionPerformed(evt);
            }
        });

        bt_lim_cargo.setText("Limpar");
        bt_lim_cargo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_lim_cargoActionPerformed(evt);
            }
        });

        jl_cad_carg.setText("Cadastrar Cargo");

        jl_nome_cargo.setText("Cargo:");

        jl_cod_cargo.setText("Código:");

        bt_cad_cargo.setText("Cadastrar");
        bt_cad_cargo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_cad_cargoActionPerformed(evt);
            }
        });

        ct_nome_cargo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ct_nome_cargoActionPerformed(evt);
            }
        });

        ct_cod_cargo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ct_cod_cargoActionPerformed(evt);
            }
        });

        bt_edit_cargo.setText("Editar");
        bt_edit_cargo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_edit_cargoActionPerformed(evt);
            }
        });

        tb_cargo.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Cargo", "Código"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tb_cargo);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(114, 114, 114)
                        .addComponent(jl_cad_carg))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(bt_cad_cargo)
                                .addGap(18, 18, 18)
                                .addComponent(bt_edit_cargo)
                                .addGap(18, 18, 18)
                                .addComponent(bt_lim_cargo)
                                .addGap(18, 18, 18)
                                .addComponent(bt_rem_cargo))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jl_nome_cargo)
                                    .addComponent(jl_cod_cargo))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(ct_cod_cargo, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(ct_nome_cargo, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 339, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(jl_cad_carg)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jl_nome_cargo)
                            .addComponent(ct_nome_cargo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jl_cod_cargo)
                            .addComponent(ct_cod_cargo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(bt_cad_cargo)
                            .addComponent(bt_edit_cargo)
                            .addComponent(bt_lim_cargo)
                            .addComponent(bt_rem_cargo))))
                .addContainerGap(78, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bt_rem_cargoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_rem_cargoActionPerformed
        
        DefaultTableModel dtm = (DefaultTableModel) tb_cargo.getModel();

        int linha = tb_cargo.getSelectedRow();
        Object[] dados = {ct_nome_cargo.getText(), ct_cod_cargo.getText()};

        dtm.removeRow(linha);
        JOptionPane.showMessageDialog(null, "Cargo removido com sucesso!", "Cadastro Cargo", JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_bt_rem_cargoActionPerformed

    private void bt_lim_cargoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_lim_cargoActionPerformed
        
        ct_nome_cargo.setText("");
        ct_cod_cargo.setText("");
    }//GEN-LAST:event_bt_lim_cargoActionPerformed

    private void bt_cad_cargoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cad_cargoActionPerformed
        
        if (linha_selec >= 0){
            
            tb_cargo.setValueAt(ct_nome_cargo.getText (), linha_selec, 0);
            tb_cargo.setValueAt(ct_cod_cargo.getText (), linha_selec, 1);
            
            ct_nome_cargo.setText("");
            ct_cod_cargo.setText("");
            
            JOptionPane.showMessageDialog(null, "Dados Atualizados com Sucesso!", "Cadastro Cargo", JOptionPane.INFORMATION_MESSAGE);
            
            linha_selec = -1;
            bt_cad_cargo.setText("Cadastrar");
            
        } else{
            
            DefaultTableModel dtm = (DefaultTableModel) tb_cargo.getModel();
            Object[] opcoes = {ct_nome_cargo.getText(), ct_cod_cargo.getText()};
            dtm.addRow(opcoes);
            
            ct_nome_cargo.setText("");
            ct_cod_cargo.setText("");
            
            JOptionPane.showMessageDialog(null, "Cargo Cadastrado com Sucesso!", "Cadastro Cargo", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_bt_cad_cargoActionPerformed

    private void ct_nome_cargoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ct_nome_cargoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ct_nome_cargoActionPerformed

    private void ct_cod_cargoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ct_cod_cargoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ct_cod_cargoActionPerformed

    private void bt_edit_cargoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_edit_cargoActionPerformed
        
        linha_selec = tb_cargo.getSelectedRow();
        
        if (linha_selec <= -1){
            
            JOptionPane.showMessageDialog(null, "Selecione Uma Linha");
        
        } else{    
            bt_cad_cargo.setText("Atualizar");
        
        ct_nome_cargo.setText(tb_cargo.getValueAt(linha_selec, 0).toString());
        ct_cod_cargo.setText(tb_cargo.getValueAt(linha_selec, 1).toString());
        }
    }//GEN-LAST:event_bt_edit_cargoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Cargo_gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Cargo_gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Cargo_gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Cargo_gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Cargo_gui().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_cad_cargo;
    private javax.swing.JButton bt_edit_cargo;
    private javax.swing.JButton bt_lim_cargo;
    private javax.swing.JButton bt_rem_cargo;
    private javax.swing.JTextField ct_cod_cargo;
    private javax.swing.JTextField ct_nome_cargo;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel jl_cad_carg;
    private javax.swing.JLabel jl_cod_cargo;
    private javax.swing.JLabel jl_nome_cargo;
    private javax.swing.JTable tb_cargo;
    // End of variables declaration//GEN-END:variables
}
